<template>
    <h3>用户理财赎回</h3>
    <table>
        <tr>
            <td>理财产品名称</td>
            <td>
                {{ data.F_Name }}
            </td>
        </tr>
        <tr>
            <td>年收益</td>
            <td>
                {{ data.F_APY }}
            </td>
        </tr>
        <tr>
            <td>最低理财金额</td>
            <td>
                {{ data.F_MinMoney.toFixed(2) }}CNY
            </td>
        </tr>
        <tr>
            <td>风险等级</td>
            <td>
                {{ data.F_Level == 0 ? '低风险' : (data.F_Level == 1 ? '中风险' : '高风险') }}
            </td>
        </tr>
        <tr>
            <td>用户购买金额</td>
            <td>
                {{ data.UF_Money.toFixed(2) }}CNY
            </td>
        </tr>
        <tr>
            <td>赎回金额</td>
            <td>
                <input type="text" v-model="data.Rmoney" />
            </td>
        </tr>
        <tr>
            <td>
                <button @click="RedeemMoney">确认赎回</button>
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
import { onMounted, ref } from 'vue'
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';

//创建路由实例
const route = useRoute();
const router = useRouter();

//创建响应对象
const data = ref({
    F_Name: '',
    F_APY: '',
    F_MinMoney: 0,
    F_Level: 0,
    UF_Money: 0,
    Rmoney: 0
});

onMounted(() => {
    axios({
        method: "get",
        url: "https://localhost:44305/api/Finans/GetUserRedeemInfo",
        params: {
            UFid: route.params.UFid
        }
    })
        .then(function (res) {
            data.value = res.data[0];
        })
        .catch(function (error) {
            console.log(error);
        })

})


//用户理财赎回
const RedeemMoney = () => {
    //判断赎回金额必须大于0
    if (!data.value.Rmoney) {
        alert('赎回金额不能为空');
        return;
    }

    if (data.value.Rmoney <= 0) {
        alert('赎回金额必须大于0元');
        return;
    }

    if (data.value.Rmoney > data.value.UF_Money) {
        alert('赎回金额必须小于理财金额');
        return;
    }

    axios({
        method: "post",
        url: "https://localhost:44305/api/Finans/AddUserFinanRedeemInfo",
        data: {
            UFR_Money: data.value.Rmoney,
            UF_Id: route.params.UFid
        }
    })
        .then(function (res) {
            if (res.data > 0) {
                alert('用户理财赎回成功');
                router.push('/UserFinanRedeemInfo');
            }
            else {
                alert('用户理财赎回失败');
                router.push('/UserFinanRedeemInfo');
            }
        })
        .catch(function (error) {
            console.log(error);
        })

}
</script>